from flask import current_app
from flask import g
from flask import session

from info import constants
import functools


def do_rank_class(index):
    if index < 0 or index >= 3:
        return ''
    rank_class_li = ['first', 'second', 'third']
    return rank_class_li[index]


def login_user_data(view_func):
    @functools.wraps(view_func)
    def wrapper(*args, **kwargs):
        # 尝试从session中获取user_id
        user_id = session.get('user_id')  # 获取不到，返回None
        user = None
        if user_id:
            # 用户已登录
            from info.models import User
            try:
                # user = User.query.get(user_id)
                # user.avatar_url = constants.QINIU_DOMIN_PREFIX + user.avatar_url
                user = User.query.get(user_id)
                user.avatar_url = constants.QINIU_DOMIN_PREFIX + user.avatar_url if user.avatar_url else ''
                # if user.avatar_url:
            #     constants.QINIU_DOMIN_PREFIX + user.avatar_url
            # else:
            #     ''

            except Exception as e:
                current_app.logger.error(e)
        g.user = user
        return view_func(*args, **kwargs)

    return wrapper
